<template>

  <div class="person-container">

    <el-page-header @back="goBack" title="返回人才列表" style="margin-left:10px;height:20px" v-if="disabled"></el-page-header>

    <div class="person-data">

      <el-form :model="personnel" :inline="true" :rules="rules" ref="personnelForm" label-width="100px" class="demo-ruleForm">

        <el-form-item label="姓名" prop="name" :label-width="label_width">
          <el-input v-model="personnel.name"  style="width:140px"></el-input>
        </el-form-item>

        <el-form-item label="年龄" prop="age" :label-width="label_width">
          <el-input-number v-model="personnel.age"  :min="18" :max="70"  style="width:160px" ></el-input-number>
        </el-form-item>

        <el-form-item label="性别" prop="sex" :label-width="label_width">
          <el-select v-model="personnel.sex" placeholder="请选择员工性别" style="width:150px" @change="refresh">
            <el-option label="男" value="0"></el-option>
            <el-option label="女" value="1"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="手机" prop="phone" :label-width="label_width">
          <el-input v-model="personnel.phone" style="width:150px"></el-input>
        </el-form-item>

        <el-form-item label="住址" prop="address" :label-width="label_width">
          <el-input v-model="personnel.address" style="width:400px"></el-input>
        </el-form-item>

        <el-form-item label="籍贯" prop="nativePlace" :label-width="label_width">
          <el-input v-model="personnel.nativePlace" style="width:400px"></el-input>
        </el-form-item>

        <el-form-item label="学历" prop="education" :label-width="label_width">
          <el-select v-model="personnel.education"  placeholder="请选择学历" style="width:150px" @change="refresh">
            <el-option label="博士" value="0"></el-option>
            <el-option label="硕士" value="1"></el-option>
            <el-option label="本科" value="2"></el-option>
            <el-option label="专科" value="3"></el-option>
            <el-option label="其他" value="4"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="政治面貌" prop="politicCountenance" :label-width="label_width">
          <el-select v-model="personnel.politicCountenance"  placeholder="请选择政治面貌" style="width:150px" @change="refresh">
            <el-option label="党员" value="0"></el-option>
            <el-option label="团员" value="1"></el-option>
            <el-option label="群众" value="2"></el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="邮箱" prop="email" :label-width="label_width">
          <el-input v-model="personnel.email" style="width:400px"></el-input>
        </el-form-item>

        <el-form-item label="毕业学校" prop="graduationSchool" :label-width="label_width">
          <el-input v-model="personnel.graduationSchool" style="width:400px"></el-input>
        </el-form-item>

        <el-form-item label="期望岗位" prop="expectedPosition" :label-width="label_width">
          <el-input v-model="personnel.expectedPosition" style="width:150px"></el-input>
        </el-form-item>

        <el-form-item label="期望薪资" prop="expectedSalary" :label-width="label_width">
          <el-input v-model="personnel.expectedSalary" style="width:150px"></el-input>
        </el-form-item>

        <el-form-item label="头像" prop="avatar" :label-width="label_width">
          <el-upload
            style="margin-left:100px;margin-right:800px"
            class="avatar-uploader"
            action="http://localhost:9000/api/fileController/oss"
            :show-file-list="false"
            :on-success="handleAvatarSuccess">
            <img v-if="personnel.avatar" :src="personnel.avatar" class="avatar" alt="">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="personnelFlag = true" style="margin-left:300px;">添加工作经历</el-button>
        </el-form-item>

        <el-table :data="personnel.works" style="width: 100%">
          <el-table-column prop="companyName" label="原公司" width="150"></el-table-column>
          <el-table-column prop="position" label="原岗位" width="150"></el-table-column>
          <el-table-column prop="workTime" label="工作时长" width="150"></el-table-column>
          <el-table-column prop="salary" label="原工资" width="150"></el-table-column>
          <el-table-column prop="dateTime" label="时长范围" width="170"></el-table-column>
        </el-table>

        <el-form-item>
          <el-button type="primary" @click="reset" style="margin-left:280px;margin-top:30px" :disabled="disabled">重置</el-button>
          <el-button type="primary" @click="addPersonnel">{{ btn }}</el-button>
        </el-form-item>
      </el-form>

    </div>


    <el-dialog title="工作经验" :visible.sync="personnelFlag" label-width="100px" width="550px">

      <el-form :model="work" :rules="work_rule" :inline="true" ref="workForm">
        <el-form-item label="原公司" :label-width="label_width" prop="companyName">
          <el-input v-model="work.companyName" autocomplete="off" style="width:350px"></el-input>
        </el-form-item>
        <el-form-item label="原岗位" :label-width="label_width"  prop="position">
          <el-input v-model="work.position" autocomplete="off" style="width:350px"></el-input>
        </el-form-item>
        <el-form-item label="工作时长" :label-width="label_width" prop="workTime" >
          <el-input v-model="work.workTime" autocomplete="off" style="width:120px"></el-input>
        </el-form-item>
        <el-form-item label="原工资" :label-width="label_width" prop="salary">
          <el-input v-model="work.salary" autocomplete="off" style="width:120px"></el-input>
        </el-form-item>
        <el-form-item label="工作时间范围" :label-width="'120px'" prop="salary">
          <el-input v-model="work.dateTime" autocomplete="off" style="width:300px"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelWork">取 消</el-button>
        <el-button type="primary" @click="addWork">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import {saveOrUpdatePersonInfo,selectPersonInfoById} from "@/api/personnel";

export default {
  name:'personnelSaveOrUpdate',
  data(){
    return{
      id:'',
      btn:'添加',
      disabled:false,
      personnel:{
        age:20,
        avatar:'',
        works:[]
      },
      work:{},
      personnelFlag:false,
      rules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        ],
        age:[
          {required: true, message: '请输入年龄', trigger: 'blur' },
          {pattern: /^[0-9]*$/g, message: '只能输入数字', trigger: 'blur'},
        ],
        sex:[
          { required: true, message: '请选择性别', trigger: 'blur' },
        ],
        phone:[
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur'}
        ],
        address:[
          { required: true, message: '请输入住址', trigger: 'blur' },
          { min: 5, max: 30, message: '长度在 5 到 30 个字符', trigger: 'blur' }
        ],
        education:[
          { required: true, message: '请选择学历', trigger: 'blur' },
        ],
        email:[
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ],
        graduationSchool:[
          { required: true, message: '请输入毕业学校', trigger: 'blur' },
          { min: 4, max: 30, message: '长度在 4 到 30 个字符', trigger: 'blur' }
        ],
        expectedPosition:[
          { required: true, message: '请输入期望岗位', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
        ],
        expectedSalary:[
          { required: true, message: '请输入期望薪资', trigger: 'blur' },
          { pattern: /^[0-9]*$/g, message: '只能输入数字', trigger: 'blur'},
        ],
        nativePlace:[
          { required: true, message: '请输入籍贯', trigger: 'blur' },
          { min: 5, max: 30, message: '长度在 5 到 30 个字符', trigger: 'blur' }
        ],
        politicCountenance:[
          { required: true, message: '请选择政治面貌', trigger: 'blur' },
        ]
      },
      work_rule:{
        companyName: [
          { required: true, message: '请输入原公司', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
        ],
        position: [
          { required: true, message: '请输入原岗位', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
        ],
        workTime: [
          { required: true, message: '请输入工作时长', trigger: 'blur' },
          { pattern: /^[0-9]*$/g, message: '只能输入数字', trigger: 'blur'}
        ],
        salary: [
          { required: true, message: '请输入原工资', trigger: 'blur' },
          { pattern: /^[0-9]*$/g, message: '只能输入数字', trigger: 'blur'}
        ],
      },
      label_width:'100px'
    }
  },

  created() {
    this.id = this.$route.params.id
    if (this.id.length>0){
      this.getByIdInfo()
      this.btn = '更新'
      this.disabled = true
    }
  },

  methods: {
    async saveOrUpdate() {
      const result = await saveOrUpdatePersonInfo(this.personnel)
      if (result.code === 200){
        const successMsg = this.id.length > 0 ? '更新成功':'添加成功'
        this.$message({message:successMsg, type: 'success'});
      }else {
        const errorMsg = this.id.length > 0 ? '更新失败':'添加失败'
        this.$message.error(errorMsg);
      }
    },
    goBack(){
      this.$router.go(-1)
    },

    refresh(){
      this.$forceUpdate()
    },

    async getByIdInfo(){
      const {data:res} = await selectPersonInfoById(this.id)
      this.personnel = res.data
      this.personnel.sex = this.personnel.sex.toString()
      this.personnel.politicCountenance = this.personnel.politicCountenance.toString()
      this.personnel.education = this.personnel.education.toString()
    },

    addPersonnel() {
      this.$refs.personnelForm.validate((valid) => {
        if (valid) {
          this.saveOrUpdate()
          this.$router.push({path:'/personnel/list'})
        } else {
          this.$message({
            message: '请按表单校验规则添加数据',
            type: 'warning'
          });
          return false;
        }
      })
    },
    reset() {
      this.$router.go(0)
    },
    addWork() {
      this.$refs.workForm.validate((valid) => {
        if (valid) {
          const data = this.work
          this.personnel.works.push(data)
          this.work = {}
          this.personnelFlag = false
        } else {
          this.$message({
            message: '请按表单校验规则添加数据',
            type: 'warning'
          });
          return false;
        }
      })
    },
    cancelWork() {
      this.work = {}
      this.personnelFlag = false
    },
    handleAvatarSuccess(res, file) {
      this.personnel.avatar = file.response;
    }
  }
}
</script>

<style scoped>
.person-container{
  position: relative;
  height:100%;
}
.person-data{
  margin:1% 25% 0 25%
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

</style>
